package controller;

import java.sql.SQLException;
import java.util.ArrayList;

import model.Customer;
import model.Service;
import db.DBConnection;
import db.DBService;
import db.IFService;

public class ServiceController
{
	private static ServiceController instance = null;

	public static ServiceController getInstance()
	{
		if(instance == null)
			instance = new ServiceController();

		return instance;
	}

	public ServiceController()
	{

	}

	public int addNewService(int reqID,boolean type,String name,int drawing_no, double price,String dimensions, int prod_quantity, String description, String deadline, Customer customer) throws SQLException
	{
		int result = -2;

		try
		{
			Service service = new Service();
			service.setName(name);
			service.setDrawingNo(drawing_no);
			service.setProdQuantity(prod_quantity);
			service.setDescription(description);
			service.setDeadline(deadline);
			service.setDimensions(dimensions);

			if(type)
				service.setPrice(price);

			service.setCustomer(customer);

			DBConnection.startTransaction();
			IFService dbService = new DBService();
			result = dbService.createServices(reqID,service, type);
			DBConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}
	public int transferServices(int reqID,boolean type,String name,int drawing_no, double price,String dimensions, int prod_quantity, String description, String deadline, Customer customer) throws SQLException
	{
		int result = -2;

		try
		{
			Service service = new Service();
			service.setName(name);
			service.setDrawingNo(drawing_no);
			service.setProdQuantity(prod_quantity);
			service.setDescription(description);
			service.setDeadline(deadline);
			service.setDimensions(dimensions);

			if(type)
				service.setPrice(price);

			service.setCustomer(customer);

			DBConnection.startTransaction();
			IFService dbService = new DBService();
			result = dbService.transferServices(reqID,service, type);
			DBConnection.commitTransaction();
		}
		catch(Exception e)
		{
			DBConnection.rollbackTransaction();
		}

		return result;
	}

	public int updateService(boolean type,int id,String name,double price,String dimensions,int drawing_no, int prod_quantity, String description, String deadline, Customer customer) throws SQLException
	{
		int result = -2;

		try
		{
			Service service = new Service();
			service.setServiceID(id);
			service.setName(name);


			service.setDimensions(dimensions);
			service.setDrawingNo(drawing_no);
			service.setProdQuantity(prod_quantity);
			service.setDescription(description);
			service.setDeadline(deadline);

			if(type)
				service.setPrice(price);

			service.setCustomer(customer);

			DBConnection.startTransaction();
			IFService dbService = new DBService();
			result = dbService.updateServices(service, type);
			DBConnection.commitTransaction();
		}
		catch(Exception e)
		{
			if(DBConnection.doDebug())
			{
				System.out.println("Could not update request, error : " + e);
				DBConnection.rollbackTransaction();
			} // end of if
		}

		return result;
	}

	public int changeStatus(int status, int id) throws SQLException
	{
		IFService dbService = new DBService();
		return dbService.changeStatus(status, id);
	}

	public ArrayList<Service> searchServicesByAllInfo(boolean type, boolean active, String search) throws SQLException
	{
		IFService dbService = new DBService();
		return dbService.searchServicesByAllInfo(type, active, search);
	}

	public ArrayList<Service> getAllServices(boolean type, int active) throws SQLException
	{
		IFService dbService = new DBService();
		return dbService.getAllServices(type, active);
	}

	public Service getOne(int id)  throws SQLException
	{
		IFService dbService = new DBService();
		return dbService.getOne(id);
	}

	public ArrayList<Service> searchServicesByCity(String city, boolean active, boolean type) throws SQLException
	{
		IFService dbService = new DBService();
		return dbService.searchServicesByCity(city, active, type);
	}

	public ArrayList<Service> searchServicesByDimension(String dimension, boolean active, boolean type) throws SQLException
	{
		IFService dbService = new DBService();
		return dbService.searchServicesByDimension(dimension, active, type);
	}

	public ArrayList<Service> searchServicesByCountry(String country, boolean active, boolean type) throws SQLException
	{
		IFService dbService = new DBService();
		return dbService.searchServicesByCountry(country, active, type);
	}

	public ArrayList<Service> searchServicesByCountryTld(String country_tld,boolean active, boolean type) throws SQLException
	{
		IFService dbService = new DBService();
		return dbService.searchServicesByCountryTld(country_tld, active, type);
	}

	public ArrayList<Service> searchServiceByPrice(double price, boolean active, boolean type)
	{
		IFService dbService = new DBService();
		return dbService.searchServiceByPrice(price, active, type);
	}

	public ArrayList<Service> searchServicesByDeadline(String deadline, boolean active, boolean type) throws SQLException
	{
		IFService dbService = new DBService();
		return dbService.searchServicesByDeadline(deadline, active, type);
	}


	public ArrayList<Service> searchServicesByCustomer(String customer, boolean active, boolean type) throws SQLException
	{
		IFService dbService = new DBService();
		return dbService.searchServicesByCustomer(customer, active, type);
	}

	public ArrayList<Service> searchServicesByDrawingNo(int drawingNo, boolean active, boolean type) throws SQLException
	{
		IFService dbService = new DBService();
		return dbService.searchServicesByDrawingNo(drawingNo, active, type);
	}

	public ArrayList<Service> searchServicesByProdQuantity(int quantity, boolean active, boolean type) throws SQLException
	{
		IFService dbService = new DBService();
		return dbService.searchServicesByProdQuantity(quantity, active, type);
	}

	public ArrayList<Service> searchServicesByName(String name, boolean active, boolean type) throws SQLException
	{
		IFService dbService = new DBService();
		return dbService.searchServicesByName(name, active, type);
	}
}